<template>
	<view class="box-project-container">
		<view class="top-box">
			<navBar title="选择项目/基地"  @goBack="goBack"></navBar>
			<view class="container-item">
				<u-tabs :list="tabList" @click="fnTabClick" :current='type' :scrollable='false' :lineWidth='120' :activeStyle="{color:'#2F80ED'}"></u-tabs>
			</view>
			<view class="container-search">
				<u-search class="search-input" placeholder="请输入关键字" @clear="clearCondition(type)" v-model="searchText" :showAction="false"  @search="queryByCondition($event,type)" @change="queryByCondition($event,type)"></u-search>
				<scroll-view scroll-y="true" class="scroll-box"
					:style="{height:`calc(100vh - ${boxHeight.navBarH + boxHeight.tabsH + boxHeight.viewH + 20}px)`}">
						<view class="data-box" v-for="item in mockData" :key="item.id">
							<view @click="selectedItem(item)">{{ item.name }}</view>
						</view>
				</scroll-view>
				
			</view>
			
		</view>
	</view>
</template>

<script>
	import searchCom from "@/components/searchCom.vue"
	import { getPmListItemListByOrgIdsByUserId, projectManageProductionList, stationProductionList } from "@/api/qualitySafetyManagement/qualitySafetyManagement.js"
	import { getStationList, mjsProject_list } from "@/api/baseManage/baseManage.js"
	export default {
		name:'projectOrStation',
		components: {
			searchCom
		},
		data() {
			return {
				// 几个盒子的高度,计算scroll-view的高度
				boxHeight: {
					navBarH: 40,
					tabsH: 40,
					viewH: 20,
				},
				searchText:null,
				tabList:[
					{
						type:0,
						name:'工程维保'
					},
					{
						type:1,
						name:'综合大修'
					},
					{
						type:2,
						name:'高端加固'
					},
					{
						type:3,
						name:'基地'
					},

				],
				type:0,
				mockData:[]
			}
		},
		onLoad(param) {
			const eventChannel = this.getOpenerEventChannel();
			this.eventChannel = eventChannel
			// console.log(param.projectOrBaseType,'-----')
			let orgId = null
			let resqParams1 = {
				currPage:1,
				pageSize:10000,
				params: {
					orgId,
					code:'gcwb',
					commonKeyword:this.searchText,
					projectStates: ["zj"]
				},
				projectId:null
			}
			let resqParams2 = {
				currPage:1,
				pageSize:10000,
				params: {
					orgId,
					pmStates:'zj',
					// projectId:uni.getStorageSync('isOwner'),
					keyword:this.searchText
				},
				projectId:null
				// projectId:uni.getStorageSync('isOwner')
			}
			let resqParams3 = {
				typeIds:[102,104],
				name:this.searchText,
				orgId,
				projectId:null
			}
			
			let resqParams4 = {
				currPage:1,
				pageSize:10000,
				params: {
					name:this.searchText,
					pmStates: ["zj"],
				},
				projectId:null
			}
			
			
			if(param &&  param.projectOrBaseType) {
				// debugger
				uni.showLoading({
					title:'加载中',
					mask:true
				})
				if(param.projectOrBaseType === '工程维保') {
					this.type = 0
					this.getTab1Data(resqParams1)
					uni.hideLoading()
				}else if(param.projectOrBaseType === '综合大修'){
					this.type = 1
					this.getTab2Data(resqParams2)
					uni.hideLoading()
				}else if(param.projectOrBaseType === '高端加固'){
					this.type = 2
					this.getTab4Data(resqParams4)
					uni.hideLoading()
				}else if(param.projectOrBaseType === '基地'){
					this.type = 3
					this.getTab3Data(resqParams3)
					uni.hideLoading()
				}
			}
			
		},
		mounted() {
			// 计算scorll-view的高度 , 用100vh减去几个不滚动盒子的高度
			const query = uni.createSelectorQuery().in(this);
			query.select('.u-navbar .u-navbar--fixed').boundingClientRect(data => {
				this.boxHeight.navBarH = data.height
			}).exec();
			query.select('.container-item').boundingClientRect(data => {
				this.boxHeight.tabsH = data.height
			}).exec();
			query.select('.search-input').boundingClientRect(data => {
				this.boxHeight.viewH = data.height
			}).exec();
			// this.getAllTabData()
		},
		methods: {
			goBack(){
				uni.navigateBack()
			},
			//点击tab事件
			fnTabClick(val) {
				this.type = val.type
				this.searchText = ''
				console.log(this.type,'搜索');
				let orgId = null
				let resqParams1 = {
					currPage:1,
					pageSize:10000,
					params: {
						orgId,
						code:'gcwb',
						commonKeyword:this.searchText,
						projectStates: ["zj"]
					},
					projectId:null
				}
				let resqParams2 = {
					currPage:1,
					pageSize:10000,
					params: {
						orgId,
						pmStates:'zj',
						// projectId:uni.getStorageSync('isOwner'),
						keyword:this.searchText
					},
					projectId:null
					// projectId:uni.getStorageSync('isOwner')
				}
				let resqParams3 = {
					orgId,
					typeIds:[102,104],
					name:this.searchText,
					projectId:null
				}
				let resqParams4 = {
					currPage:1,
					pageSize:10000,
					params: {
						name:this.searchText,
						pmStates: ["zj"],
					},
					projectId:null
				}
				uni.showLoading({
					title:'加载中',
					mask:true
				})
				if(this.type === 0) {
					this.getTab1Data(resqParams1)
					uni.hideLoading()
				}else if(this.type === 1) {
					this.getTab2Data(resqParams2)
					uni.hideLoading()
				}else if(this.type === 2){
					this.getTab4Data(resqParams4)
					
					uni.hideLoading()
				}else if(this.type === 3){
					this.getTab3Data(resqParams3)
					uni.hideLoading()
				}
			},
			selectedItem(item) {
				uni.navigateBack({
					success: () => {
						this.eventChannel.emit("setData", {
							data: item
						})
					}
				});
			},
			getTab1Data(resq) {
				projectManageProductionList(resq).then(res => {
					if(!res.success) return
					this.recordMockData = res.result.list.map(it => ({...it, type:'工程维保', name:it.projectName}))
					this.mockData = JSON.parse(JSON.stringify(this.recordMockData))
				})
			},
			getTab2Data(resq) {
				getPmListItemListByOrgIdsByUserId(resq).then(res => {
					if(!res.success) return
					this.mockData = res.result.list.map(it => ({...it, type:'综合大修'}))
				})
			},
			getTab3Data(resq) {
				getStationList(resq).then(res => {
					if(!res.success) return
					this.mockData = res.result.map(it => ({...it, type:'基地'}))
				})
			},
			getTab4Data(resq) {
				mjsProject_list(resq).then(res => {
					if(!res.success) return
					this.mockData = res.result.list.map(it => ({...it, type:'高端加固'}))
				})
			},
			/**
			 * @param {String} val 失焦后的值
			 * @param {String} type 当前tab
			 */
			queryByCondition(val, type) {
				console.log(val, type,'====');
				let orgId = null
				this.searchText = val
				let resqParams1 = {
					currPage:1,
					pageSize:10000,
					params: {
						orgId,
						code:'gcwb',
						commonKeyword:this.searchText,
						projectStates: ["zj"]
					},
					projectId:null
				}
				let resqParams2 = {
					currPage:1,
					pageSize:10000,
					params: {
						orgId,
						pmStates:'zj',
						// projectId:uni.getStorageSync('isOwner'),
						keyword:this.searchText
					},
					projectId:null
					// projectId:uni.getStorageSync('isOwner')
				}
				let resqParams3 = {
					typeIds:[102,104], //过滤出基地和布点
					name:this.searchText,
					orgId,
					projectId:null
				}
				
				let resqParams4 = {
					currPage:1,
					pageSize:10000,
					params: {
						name:this.searchText,
						pmStates: ["zj"],
					},
					projectId:null
				}
				if(type === 0) {
					this.getTab1Data(resqParams1)
				}else if(type === 1) {
					this.getTab2Data(resqParams2)
				}else if(type === 2){
					this.getTab4Data(resqParams4)
				}else if(type === 3){
					this.getTab3Data(resqParams3)
					
				}
			},
			clearCondition(type){
				this.searchText = null
				let orgId = null
				let resqParams1 = {
					currPage:1,
					pageSize:10000,
					params: {
						orgId,
						code:'gcwb',
						commonKeyword:this.searchText,
						projectStates: ["zj"]
					},
					projectId:null
				}
				let resqParams2 = {
					currPage:1,
					pageSize:10000,
					params: {
						orgId,
						pmStates:'zj',
						// projectId:uni.getStorageSync('isOwner'),
						keyword:this.searchText
					},
					projectId:null
					// projectId:uni.getStorageSync('isOwner')
				}
				let resqParams3 = {
					orgId,
					typeIds:[102,104],
					name:this.searchText,
					projectId:null
				}
				let resqParams4 = {
					currPage:1,
					pageSize:10000,
					params: {
						name:this.searchText,
						pmStates: ["zj"],
					},
					projectId:null
				}
				if(type === 0) {
					this.getTab1Data(resqParams1)
				}else if(type === 1) {
					this.getTab2Data(resqParams2)
				}else if(type === 2){
					this.getTab4Data(resqParams4)
				}else if(type === 3) {
					this.getTab3Data(resqParams3)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box-project-container {
		height: 100vh;
		/* #ifdef MP-WEIXIN */
			padding-top: 200rpx;
		/* #endif */
		.container-search {
			
			padding: 0 30rpx;
			.search-input {
				margin: 20rpx 0 !important;
			}
			.data-box {
				padding: 30rpx 0;
				border-bottom: 4rpx solid #f4f6f9;
				font-size: 32rpx;
			}
			.data-box:last-child {
				border-bottom: none;
			}
		}
	}
	/deep/ .u-search__content {
		height:88rpx
	}
</style>